package jdk21.virtualThread;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.stream.IntStream;

/**
 * @description: 虚拟线程
 * @author: grf
 * @date: 2024/8/22
 */
public class VirtualThreadExample3 {
    public static void main(String[] args) {
        // 自定义线程工厂
        ThreadFactory customThreadFactory = r -> {
            Thread thread = new Thread(r);
            thread.setName("CustomVirtualThread-" + thread.threadId());
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.setDaemon(true);
            return thread;
        };

        try (var executor = Executors.newThreadPerTaskExecutor(customThreadFactory)) {
            IntStream.range(0, 10).forEach(i -> executor.submit(() -> {
                System.out.println("Task " + i + " is running in: " + Thread.currentThread());
                // 执行任务
            }));
        }
    }
}
